package com.nineclock.doc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.doc.dto.DocFileDto;
import com.nineclock.doc.dto.DocFolderDto;
import com.nineclock.doc.dto.DocumentDto;
import com.nineclock.doc.mapper.DocumentFileMapper;
import com.nineclock.doc.mapper.DocumentFolderMapper;
import com.nineclock.doc.pojo.DocFile;
import com.nineclock.doc.pojo.DocFolder;
import com.nineclock.doc.service.DocumentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DocumentServiceImpl implements DocumentService {
    @Autowired
    private DocumentFileMapper fileMapper;
    @Autowired
    private DocumentFolderMapper folderMapper;

    /**
     * 查询当前目录下的文件夹及文件
     *
     * @param parentFoldId
     * @param keyword
     * @return
     */
    @Override
    public DocumentDto queryFileAndFolder(String parentFoldId, String keyword) {
        if (StrUtil.isEmpty(parentFoldId)) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //返回值对象
        DocumentDto documentDto = new DocumentDto();
        List<DocFolderDto> folders = new ArrayList<>();
        List<DocFileDto> files = new ArrayList<>();

        this.getChildFolder(parentFoldId, folders, files);

        documentDto.setFolders(folders);
        //去重
        documentDto.setFiles(CollectionUtil.distinct(files));
        return documentDto;
    }

    /**
     * 查询子文件夹
     *
     * @param parentFoldId
     * @return
     */
    public void getChildFolder(String parentFoldId, List<DocFolderDto> resultList, List<DocFileDto> files) {

        //查询该文件夹下的文件,公共读，公共读写
        LambdaQueryWrapper<DocFile> fileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fileLambdaQueryWrapper.eq(DocFile::getCompanyId, CurrentUserHolder.get().getCompanyId());
        fileLambdaQueryWrapper.eq(DocFile::getFolderId, parentFoldId);
        fileLambdaQueryWrapper.eq(DocFile::getStatus, 1);
        fileLambdaQueryWrapper.in(DocFile::getPermission, 0, 1);
        List<DocFile> docFileList = fileMapper.selectList(fileLambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(docFileList)) {
            files.addAll(BeanHelper.copyWithCollection(docFileList, DocFileDto.class));
        }
        //查询员工私有文件
        LambdaQueryWrapper<DocFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocFile::getCompanyId, CurrentUserHolder.get().getCompanyId());
        wrapper.eq(DocFile::getFolderId, parentFoldId);
        wrapper.eq(DocFile::getStatus, 1);
        wrapper.eq(DocFile::getPermission, 2); //2为私有文件
        wrapper.eq(DocFile::getCreateUserId, CurrentUserHolder.get().getCompanyUserId());
        List<DocFile> privateFiles = fileMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(privateFiles)) {
            files.addAll(BeanHelper.copyWithCollection(privateFiles, DocFileDto.class));
        }

        //查询该用户作为协作者文件
        Long companyUserId = CurrentUserHolder.get().getCompanyUserId();
        List<DocFile> collaborationFiles = fileMapper.queryUserCollaborationFile(companyUserId);
        if (CollectionUtil.isNotEmpty(collaborationFiles)) {
            files.addAll(BeanHelper.copyWithCollection(collaborationFiles, DocFileDto.class));
        }


        //查询该文件夹下的子文件夹
        LambdaQueryWrapper<DocFolder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DocFolder::getParentId, parentFoldId);
        lambdaQueryWrapper.eq(DocFolder::getCompanyId, CurrentUserHolder.get().getCompanyId());
        lambdaQueryWrapper.eq(DocFolder::getStatus, 1);
        List<DocFolder> docFolderList = folderMapper.selectList(lambdaQueryWrapper);


        if (CollectionUtil.isNotEmpty(docFolderList)) {
            List<DocFolderDto> docFolderDtoList = BeanHelper.copyWithCollection(docFolderList, DocFolderDto.class);
            resultList.addAll(docFolderDtoList);
           /* for (DocFolder docFolder : docFolderList) {
                this.getChildFolder(docFolder.getId()+"",resultList,files);
            }*/
        }
    }
}
